package com.xh.bussiness.practice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.xh.bussiness.practice.bean.dto.PracticeStudentVO;
import com.xh.bussiness.practice.bean.request.PracticeStudentResultRequest;
import com.xh.bussiness.practice.model.PracticeStudentResult;
import com.xh.bussiness.practice.dao.PracticeStudentResultMapper;
import com.xh.bussiness.practice.service.IPracticeStudentResultService;
import com.xh.bussiness.tiku.bean.dto.QuestionDto;
import com.xh.bussiness.tiku.service.IQuestionService;
import com.xh.bussiness.tms.model.ClassStudent;
import com.xh.bussiness.tms.service.IClassStudentService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 学生作业作答结果 服务实现类
 * </p>
 *
 * @author Pitt
 * @since 2021-01-26
 */
@Service
public class PracticeStudentResultServiceImpl extends BaseServiceImpl<PracticeStudentResultMapper, PracticeStudentResult> implements IPracticeStudentResultService {
    @Autowired
    IClassStudentService studentService;
    @Autowired
    private IQuestionService questionService;
    @Override
    public XhPage queryPracticeStudentResultPage(XhPage page, PracticeStudentResultRequest bean) {
        QueryWrapper qw=new QueryWrapper<PracticeStudentResult>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List<PracticeStudentResult> queryPracticeStudentResult(PracticeStudentResultRequest bean) {
        QueryWrapper qw=new QueryWrapper<PracticeStudentResult>();
        //拼装条件
        qw=prepareParams(bean, qw);
        qw.orderByAsc("order_no");
        return this.dao.selectList(qw);
    }

    @Override
    public void deleteByPracticeId(String practiceId) {
        this.dao.deleteByPracticeId(practiceId);
    }


    @Override
    public void deleteByPracticeIdAndStuId(String practiceId,String stuId) {
        this.dao.deleteByPracticeIdAndStuId(practiceId,stuId);
    }

    @Override
    public Integer getPracticeQuestionCount(String practiceId, String studentId) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("practice_id", practiceId);
        qw.eq("student_id", studentId);
        Integer count = this.dao.selectCount(qw);
        return count == null ? 0 : count;
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(PracticeStudentResultRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if (!StringUtils.isEmpty(bean.getStudentId())) {
            qw.eq("student_id", bean.getStudentId());
        }
        if (!StringUtils.isEmpty(bean.getPracticeId())) {
            qw.eq("practice_id", bean.getPracticeId());
        }
        return qw;
    }

    @Override
    public List<PracticeStudentResult> queryStudentScore(String practiceId) {
        return this.dao.queryStudentScore(practiceId);
    }

    /**
     * 查询题库作业平均分提交人数低于平均分的人数
     * @author 赵文博
     * @date 2021/5/18 10:49
     * @param practiceId
     * @param questionId
     * @return com.xh.bussiness.practice.bean.dto.PracticeStudentVO
     **/
    @Override
    public PracticeStudentVO queryQuestionAnalysis(String practiceId, String questionId) {
        PracticeStudentVO studentVO = this.dao.queryQuestionAnalysis(practiceId,questionId);
        //如果当前作业 没有学生 则全为0
        if(studentVO!=null && studentVO.getAvgScore()==null){
            studentVO.setAvgScore(0.0);
            studentVO.setBelowAvg(0);
        }else if(studentVO!=null && studentVO.getAvgScore()!=null){
            Integer belowAvgCount = this.dao.queryBelowAvg(practiceId,questionId,studentVO.getAvgScore());
            studentVO.setBelowAvg(belowAvgCount);
        }
        return studentVO;
    }

    /**
     * 查询普通作业平均分提交人数低于平均分的人数
     * @author 赵文博
     * @date 2021/5/18 10:50
     * @param practiceId
     * @return com.xh.bussiness.practice.bean.dto.PracticeStudentVO
     **/
    @Override
    public PracticeStudentVO queryPracticeAnalysis(String practiceId) {
        PracticeStudentVO studentVO = this.dao.queryPracticeAnalysis(practiceId);
        if(studentVO!=null && studentVO.getAvgScore()==null){
            studentVO.setAvgScore(0.0);
            studentVO.setBelowAvg(0);
        }else if(studentVO!=null && studentVO.getAvgScore()!=null){
            Integer belowAvgCount = this.dao.queryPracticeBelowAvg(practiceId,studentVO.getAvgScore());
            studentVO.setBelowAvg(belowAvgCount);
        }
        return studentVO;
    }

    /**
     * 高频错题个数
     * @author 赵文博
     * @date 2021/4/27 16:20
     * @param classId
     * @return java.lang.Integer
     **/
    @Override
    public HashMap<String,Integer> queryHighFrequencyErrorQuestionCount(String classId) {
        HashMap<String,Integer> map = new HashMap<>();
        Integer count =  queryHighFrequencyErrorQuestion(classId,null).size();
        map.put("count",count);
        return map;
    }

    /**
     * 高频错题列表
     * @author 赵文博
     * @date 2021/4/27 16:42
     * @param classId
     * @return java.util.List<com.xh.bussiness.tiku.bean.dto.QuestionDto>
     **/
    @Override
    public List<QuestionDto> queryHighFrequencyErrorQuestions(String classId,String basicType) {
        List<String> list = queryHighFrequencyErrorQuestion(classId,basicType);
        List<QuestionDto> questionDtoS = new ArrayList<>();
        for (String id : list){
            QuestionDto questionDto = questionService.getQuestion(id,null);
            questionDtoS.add(questionDto);
        }
        return questionDtoS;
    }

    /**
     * 高频错题
     * @author 赵文博
     * @date 2021/4/27 10:50
     * @param classId
     * @return java.util.List<java.lang.String>
     **/
    @Override
    public List<String> queryHighFrequencyErrorQuestion(String classId,String basicType) {
        //获取班级60%的人数
        QueryWrapper<ClassStudent> qw = new QueryWrapper();
        qw.eq("class_id",classId);
        qw.eq("deleted",0);
        Integer studentCount = studentService.selectCount(qw);
        double refer= Math.ceil(studentCount*0.4);
        //查询一个月内学生的错题 按学生去重
        LocalDate now = LocalDate.now();
        LocalDate minusMonth = now.minusMonths(1);
        String nowStr = now.toString()+" 23:59:59";
        String minusMonthStr = minusMonth.toString()+" 00:00:00";
        List<String> questionIds = this.dao.queryErrorQuestionByClassId(classId,nowStr,minusMonthStr,basicType);
        //某一题做错人数超过60%即为高频错题
        Map<String,Long> map =  questionIds.stream().collect(Collectors.groupingBy(q->q,Collectors.counting()));
        List<String> list = new ArrayList<>();
        for(String key : map.keySet()){
            Long value = map.get(key);
            if(value>=refer){
                list.add(key);
            }
        }
        return list;
    }
}
